<?php
namespace Home\Controller;
use Think\Controller;
/*
 *总后台加盟商家控制器
 */
class ShopController extends HomeController {
		/*
		 *总后台加盟商家列表
		 */
		public function index()
		{
			//获取所有上架产品
			$page=I('get.page','1','strip_tags');
	        $count=M('shop_user')->count();
	        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
			$result=M('shop_user a')->join('inner join '.C('DB_PREFIX').'yunying_user  b on a.founder=b.yid') 
			        ->limit($pagearr['start'],$pagearr['pagesize'])
			        ->order('a.shopid desc')
			        ->getField('a.shopid,a.companyname,a.connectname,a.address,a.mobile,a.createtime,a.money,b.companyname as yname ');
			 $device=M('device');
			 foreach ($result as $key => &$value) {
			 	$value['isfen']=$device->where('shopid=%d',$value['shopid'])->count();
			 }
			$this->assign('shops',$result);
			$this->assign('pagearr',$pagearr);
			$this->display('index');	
		}
		/*
		 *总后台加盟商编辑
		 */
		public function editShop(){
			$shopid=I('get.shopid','','strip_tags');
			//获取此商家的信息
			$shop_user=M('shop_user');
			$shop=$shop_user->where('shopid=%d AND isdel=%d',$shopid,0)->select();
			//获取企业类型
			$companytype=M('company_type')->where('is_display=%d',0)->select();
			//获取角色类型
			$roletype=M('role')->where('roleid=%d',2)->select();
			$this->assign('shop',$shop[0]);
			$this->assign('companytype',$companytype);
			$this->assign('roletype',$roletype);
			$this->display('edit');
		}
		/*
	 *保存修改后的加盟商
	 *@param shopid 商家id
	 */
	public function updateShop(){
		//接受参数
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['connectname']=I('post.connectname','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['username']=I('post.username','','strip_tags');
		$data['showname']=I('post.showname','','strip_tags');
		$data['umobile']=I('post.umobile','','strip_tags');
		$data['uemail']=I('post.uemail','','strip_tags');
		$data['uaddress']=I('post.uaddress','','strip_tags');
		$data['upassword']=I('post.upassword','','strip_tags');
		$data['againpassword']=I('post.againpassword','','strip_tags');
		$data['createtime']=time();
	//	$data['founder']=!empty($_SESSION['yid'])?$_SESSION['yid']:0;//运营商id
		$data['urole']=I('post.urole','','strip_tags');
		$data['cateid']=I('post.cateid','','strip_tags');//企业类型
		$data['shopid']=I('post.shopid','','strip_tags');
		if(empty($data['companyname'])){

			 echo "<script type='text/javascript'>alert('用户名不能为空'); window.history.go(-1);window.location.reload();</script>";
		}
		//判断两次输入的密码是否相同
		if($data['upassword'] !==$data['againpassword']){
			echo "<script type='text/javascript'>alert('两次输入的密码不相同'); window.history.go(-1);window.location.reload();</script>";
		}
		//判断密码是否修改过
		$shop_user=M('shop_user');
		$shoppassword= $shop_user->where()->getField('password');
		if($data['upassword'] !=$shoppassword){
			$data['upassword']=md5($data['upassword']);
		}
		$result=$shop_user->where('shopid=%d',$data['shopid'])->save($data);
		if($result !==false){
			echo "<script type='text/javascript'>alert('修改成功'); window.location.href='./index';</script>";
		}else{
			echo "<script type='text/javascript'>alert('修改失败'); window.location.href='./index';</script>";
		}
		
	} 

	/*
	 *搜索加盟商
	 *keywords 运营商关键词
	 *return json
	 */
	public function searShop(){
		
		$arg=array();
		$arg['keywords']=I('post.keywords','','strip_tags');
		
		$shop=D('ShopUser');

		$users=$shop->index($arg);

		$result=array();

		$result['status']=!empty($users)?1:0;

		$result['users']=$users;

		echo json_encode($result);

		return '';
	} 
	

	}